package com.example.myapplication.Garbagesorting;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.CarouselGarbageBean;
import com.example.myapplication.ShuJu.GarbagesortingBean;
import com.example.myapplication.ShuJu.NewsCategoriesBean;
import com.example.myapplication.ShuJu.SearchpicturegridBean;
import com.example.myapplication.apter.SearchpicturegridApter;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import java.time.temporal.ValueRange;

import kotlin.reflect.KVariance;

public class SortSearchActivity extends AppCompatActivity {

    private Button but_sort_search;
    private ImageView img_sort_search;
    private TabLayout tab_sort_search;
    private Button gr_but01;
    private Button gr_but02;
    private Button gr_but03;
    private Button gr_but04;
    private String butName01;
    private String butName02;
    private String butName03;
    private String butName04;
    private RecyclerView re_SortSearchActivity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sort_search);
        inivview();
        //返回
        but_bloak();
        //获取所有垃圾分类
        garbagesorting();
        //垃圾宫格
        picturegrid();

    }

    private void garbagesorting() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/garbage-classification/list",
                OKHttpTool.GET,
                GarbagesortingBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        GarbagesortingBean  garbagesortingBean = (GarbagesortingBean)obj;
                        if(garbagesortingBean.getCode()==200){
                           runOnUiThread(new Runnable() {
                               @Override
                               public void run() {
                                   //进入分类的介绍
                                   for (GarbagesortingBean.RowsBean row : garbagesortingBean.getRows()) {
                                       if(row.getName().equals(butName01)){
                                           gr_but01.setOnClickListener(new View.OnClickListener() {
                                               @Override
                                               public void onClick(View v) {
                                                   enterintroduction(row);
                                               }
                                           });
                                       }else  if(row.getName().equals(butName02)){
                                           gr_but02.setOnClickListener(new View.OnClickListener() {
                                               @Override
                                               public void onClick(View v) {
                                                   enterintroduction(row);
                                               }
                                           });
                                       }else  if(row.getName().equals(butName03)){
                                           gr_but03.setOnClickListener(new View.OnClickListener() {
                                               @Override
                                               public void onClick(View v) {
                                                   enterintroduction(row);
                                               }
                                           });
                                       }else  if(row.getName().equals(butName04)){
                                           gr_but04.setOnClickListener(new View.OnClickListener() {
                                               @Override
                                               public void onClick(View v) {
                                                   enterintroduction(row);
                                               }
                                           });
                                       }
                                   }
                               }
                           });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void picturegrid() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/garbage-example/list",
                OKHttpTool.GET,
                SearchpicturegridBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        SearchpicturegridBean searchpicturegridBean = (SearchpicturegridBean)obj;
                        if(searchpicturegridBean.getCode()==200){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    SearchpicturegridApter searchpicturegridApter = new SearchpicturegridApter(SortSearchActivity.this,searchpicturegridBean.getRows(),20);
                                    GridLayoutManager gridLayoutManager = new GridLayoutManager(SortSearchActivity.this,4);
                                    re_SortSearchActivity.setAdapter(searchpicturegridApter);
                                    re_SortSearchActivity.setLayoutManager(gridLayoutManager);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void enterintroduction(GarbagesortingBean.RowsBean garbagesortingBean) {
        Intent intent = new Intent(SortSearchActivity.this,CategorypageActivity.class);
        Gson gson = new Gson();
        String garbagesortingBeanName   = gson.toJson(garbagesortingBean);
        intent.putExtra("GarbagesortingBean",garbagesortingBeanName);
        SortSearchActivity.this.startActivity(intent);
    }

    private void but_bloak() {
        but_sort_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SortSearchActivity.this.finish();
            }
        });
    }

    private void inivview() {
        Intent intent = getIntent();
        String newsCategoriesBeanStringID = intent.getStringExtra("carouselGarbageBeanStringID");
        Gson gson = new Gson();
        CarouselGarbageBean.DataBean carouselGarbageBean = gson.fromJson(newsCategoriesBeanStringID, CarouselGarbageBean.DataBean.class);

        but_sort_search = findViewById(R.id.but_sort_search);
        img_sort_search = findViewById(R.id.img_sort_search);

        gr_but01 = findViewById(R.id.gr_but01);
        gr_but02 = findViewById(R.id.gr_but02);
        gr_but03 = findViewById(R.id.gr_but03);
        gr_but04 = findViewById(R.id.gr_but04);
        re_SortSearchActivity = findViewById(R.id.re_SortSearchActivity);


        butName01 = gr_but01.getText().toString();
        butName02 = gr_but02.getText().toString();
        butName03 = gr_but03.getText().toString();
        butName04 = gr_but04.getText().toString();



        String imgUrl = carouselGarbageBean.getImgUrl();
        Glide.with(SortSearchActivity.this).load(TokenBean.IP+imgUrl).into(img_sort_search);
    }
}